﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


    public class PropertyHelper
    {



    /// <summary>
    /// 返回全部列名的属性数组
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="cols"></param>
    /// <returns></returns>
    public static PropertyInfo[] GetTypeProperties<T>()
    {
        Type type = typeof(T);
        //获取所有属性
        PropertyInfo[] properties = type.GetProperties();
        return properties;
    }

    /// <summary>
    /// 返回指定类型的指定列名的属性数组
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="cols"></param>
    /// <returns></returns>
    public static PropertyInfo[] GetTypeProperties<T>(string cols)
    {
        //Type type = typeof(T);
        //将列名字符串转换成List,转换成小写
        List<string> listCols = GetStrList(cols, ',', true);
        //获取所有属性
        PropertyInfo[] properties = GetTypeProperties<T>();
        if (listCols != null && listCols.Count > 0)
        {
            properties = properties.Where(p => listCols.Contains(AttributeHelper.GetColName(p).ToLower())).ToArray();
        }
        return properties;

    }


    ///// <summary>
    ///// 返回指定类型的指定列名的属性数组
    ///// </summary>
    ///// <typeparam name="T"></typeparam>
    ///// <param name="cols"></param>
    ///// <returns></returns>
    //public static PropertyInfo[] GetTypeProperties<T>(string cols)
    //    {
    //        //Type type = typeof(T);
    //        //将列名字符串转换成List,转换成小写
    //        List<string> listCols = GetStrList(cols,',', true);
    //        //获取所有属性
    //        PropertyInfo[] properties = GetTypeProperties<T>();
    //        if (listCols != null && listCols.Count > 0)
    //        {
    //            properties = properties.Where(p => listCols.Contains(GetColName(p).ToLower())).ToArray();
    //        }
    //        return properties;

    //    }



    /// <summary>
    /// 把字符串按照分隔符转换成 List
    /// </summary>
    /// <param name="str">源字符串</param>
    /// <param name="speater">分隔符</param>
    /// <param name="toLower">是否转换为小写</param>
    /// <returns></returns>
    public static List<string> GetStrList(string str, char speater, bool toLower)
    {
        List<string> list = new List<string>();
        string[] ss = str.Split(speater);
        foreach (string s in ss)
        {
            if (!string.IsNullOrEmpty(s) && s != speater.ToString())
            {
                string strVal = s;
                if (toLower)
                {
                    strVal = s.ToLower();
                }
                list.Add(strVal);
            }
        }
        return list;
    }


}
